package com.example.demo.socket;

import java.io.*;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Scanner;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2021年01⽉12⽇<br/>
 * @version：v1.0.0<br>
 */
public class SocketClient {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket();
        // 超时时间
        socket.setSoTimeout(30000);
        // 连接本地，端口2000；超时时间3000ms
        socket.connect(new InetSocketAddress(Inet4Address.getLocalHost(), 8000), 3000);
        System.out.println("已发起服务器连接，并进入后续流程～");
        System.out.println("客户端信息：" + socket.getLocalAddress() + " Port:" + socket.getLocalPort());
        System.out.println("服务器信息：" + socket.getInetAddress() + " Port:" + socket.getPort());
        try {
            // 发送接收数据
            todo(socket);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 释放资源
//        socket.close();
//        System.out.println("客户端已退出～");
    }

    private static void todo(Socket client) throws IOException {
        // 构建键盘输入流
        Scanner scanner = new Scanner(System.in);
        // 得到Socket输出流，并转换为打印流
        OutputStream outputStream = client.getOutputStream();
        PrintStream socketPrintStream = new PrintStream(outputStream);
        // 得到Socket输入流，并转换为BufferedReader
        InputStream inputStream = client.getInputStream();
        BufferedReader socketBufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        boolean flag = true;
        do {
            // 键盘读取一行，发送到服务器
            String str = scanner.nextLine();
            socketPrintStream.println(str);
            // 从服务器读取一行
            String echo = socketBufferedReader.readLine();
            if ("bye".equalsIgnoreCase(echo)) {
//                flag = false;
                System.out.println(echo);
            } else {
                System.out.println(echo);
            }
        } while (flag);
        // 资源释放
        socketPrintStream.close();
        socketBufferedReader.close();
    }
}
